---
title: ValyuContext
---

>[Valyu](https://www.valyu.network/) allows AI applications and agents to search the internet and proprietary data sources for relevant LLM ready information.

This notebook goes over how to use Valyu context tool in LangChain.

First, get an Valyu API key and add it as an environment variable. Get $10 free credit  by [signing up here](https://platform.valyu.network/).

## Overview

### Integration details

| Class                                                         | Package                                                        | Serializable | JS support |  Version |
|:--------------------------------------------------------------|:---------------------------------------------------------------| :---: | :---: | :---: |
| [Valyu Search](https://github.com/valyu-network/langchain-valyu) | [langchain-valyu](https://pypi.org/project/langchain-valyu/) | ✅ | ❌  |  ![PyPI - Version](https://img.shields.io/pypi/v/langchain-valyu?style=flat-square&label=%20) |

## Setup

The integration lives in the `langchain-valyu` package.

```python
%pip install -qU langchain-valyu
```

In order to use the package, you will also need to set the `VALYU_API_KEY` environment variable to your Valyu API key.

```python
import getpass
import os

if not os.environ.get("VALYU_API_KEY"):
    os.environ["VALYU_API_KEY"] = getpass.getpass("Valyu API key:\n")
```

## Instantiation

Here we show how to instantiate an instance of the Valyu search tool. This tool allows you to complete search queries using Valyu's Context API endpoint.

```python
from langchain_valyu import ValyuSearchTool

tool = ValyuSearchTool()
```

## Invocation

### Invoke directly with args

The Valyu search tool accepts the following arguments during invocation:

- `query` (required): A natural language search query
- `search_type` (optional): Type of search, e.g., "all"
- `max_num_results` (optional): Maximum number of results to return
- `similarity_threshold` (optional): Similarity threshold for results
- `query_rewrite` (optional): Whether to rewrite the query
- `max_price` (optional): Maximum price for the search

For reliability and performance reasons, certain parameters may be required or restricted. See the [Valyu API documentation](https://docs.valyu.network/overview) for details.

```python
search_results = tool._run(
    query="What are agentic search-enhanced large reasoning models?",
    search_type="all",
    max_num_results=5,
    similarity_threshold=0.4,
    query_rewrite=False,
    max_price=20.0,
)

print("Search Results:", search_results)
```

## Use within an agent

We can use our tools directly with an agent executor by binding the tool to the agent. This gives the agent the ability to dynamically set the available arguments to the Valyu search tool.

```python
if not os.environ.get("OPENAI_API_KEY"):
    os.environ["OPENAI_API_KEY"] = getpass.getpass("OPENAI_API_KEY:\n")
```

```python
# | output: false
# | echo: false

# !pip install -qU langchain langchain-openai
from langchain.chat_models import init_chat_model

llm = init_chat_model(model="gpt-4o", model_provider="openai", temperature=0)
```

```python
from langchain_valyu import ValyuSearchTool
from langchain.agents import create_agent

valyu_search_tool = ValyuSearchTool()

agent = create_agent(llm, [valyu_search_tool])

user_input = "What are the key factors driving recent stock market volatility, and how do macroeconomic indicators influence equity prices across different sectors?"

for step in agent.stream(
    {"messages": user_input},
    stream_mode="values",
):
    step["messages"][-1].pretty_print()
```

## API reference

For detailed documentation of all Valyu Context API features and configurations head to the API reference: [docs.valyu.network/overview](https://docs.valyu.network/overview)
